#include "Linkedlist.h"
#include <iostream>
using namespace std;

void intersect(LinkedList<int> *list1, LinkedList<int> *list2)
{
    Node<int> *current1 = list1->head;
    Node<int> *current2 = list2->head;
    while (current1 != NULL && current2 != NULL)
    {
        if (current1->data == current2->data)
        {
            cout << current1->data << ' ';
            current1 = current1->next;
            current2 = current2->next;
        }
        else if (current1->data < current2->data)
        {
            current1 = current1->next;
        }
        else if (current1->data > current2->data)
        {
            current2 = current2->next;
        }
    }
}

int main()
{
    int size1, size2;
    LinkedList<int> *list1 = new LinkedList<int>;
    LinkedList<int> *list2 = new LinkedList<int>;

    cin >> size1;
    for (int i = 0; i < size1; i++)
    {
        int temp;
        cin >> temp;
        list1->push(temp);
    }

    cin >> size2;
    for (int i = 0; i < size2; i++)
    {
        int temp;
        cin >> temp;
        list2->push(temp);
    }

    intersect(list1, list2);

    return 0;
}